home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Function in private part?
- Date: 8 Feb 1996 14:17:52 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4fd0mg$hf3@dawn.mmm.com>
- References: <4f7s5g$ntt@mcmail.CIS.McMaster.CA>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Hong Shen (g9326161@mcmail.cis.McMaster.CA) wrote:
- > Is there any reasons to make a member function in the private section of
- > a class virtual?
-
- The decisions about whether to make a function private or virtual are
- orthogonal. You make a function private if you want no derived classes
- to call it. You make a function virtual if you want to allow derived
- classes to override it.
-
- This may seem like a contradiction, but consider this example:
-
- class Mutex
- {
- public:
- ...
- void lock() { doSomeStuff(); do_lock(); doSomeOtherStuff(); }
- ...
- private:
- void doSomeStuff();
- void doSomeOtherStuff();
- virtual void do_lock() = 0;
- };
-
- In this example, the intent is that a derived class must provide the
- do_lock() function, but that the only way to call it is through the
- lock() function. Of course there is no way to prevent the derived
- class from calling do_lock() directly, but at least further derivatives
- will be unable to call it.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-